package com.p2p.base.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author leiruiqi
 *
 */
public class LoginUtil {

	public static final ThreadLocal<Object> currentUser = new ThreadLocal<Object>();
	
	
	//public static final ThreadLocal<Object> currentBackendUser = new ThreadLocal<Object>();
	//public static final ThreadLocal<String> loginName = new ThreadLocal<String>();
	
	/*public static boolean validateCookie(HttpServletRequest req, HttpServletResponse res,String cookieName){
		boolean result = false;
		String value = CookieWrapper.getCookieValue(req, cookieName);
		if(value!=null){
			
			result = true;
			LoginUser user = new LoginUser();
			user.setName(value);
			setCurrentUser(user);
		}
		
		return result;
	}*/
	
	public static String getCookieValue(HttpServletRequest req, HttpServletResponse res,String cookieName){
		String value = CookieWrapper.getCookieValue(req, cookieName);
		return value;
	}
	
	
	
	public static void setCurrentUser(Object user){
		currentUser.set(user);
		
	}
	
	public static Object getCurrentUser(){
		return currentUser.get();
	}

/*	public static Object getCurrentbackenduser() {
		return currentBackendUser.get();
	}
	
	public static void setCurrentbackenduser(Object user){
		currentBackendUser.set(user);
	}*/
	
	public static String getRemoteIp(HttpServletRequest request){
    	String remoteIp = request.getHeader("x-forwarded-for");
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getHeader("X-Real-IP");
    	}
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getHeader("Proxy-Client-IP");
    	}
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getHeader("WL-Proxy-Client-IP");
    	}
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getHeader("HTTP_CLIENT_IP");
    	}
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getHeader("HTTP_X_FORWARDED_FOR");
    	}
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getRemoteAddr();
    	}
    	if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {
    		remoteIp= request.getRemoteHost();
    	}
    	if (remoteIp != null && remoteIp.indexOf(",") != -1) {
    		remoteIp = remoteIp.substring(remoteIp.lastIndexOf(",") + 1, remoteIp.length()).trim();
    	}
        return remoteIp;
    }
}
